"use strict";

var serve = {};
(function(_) {
	_.request = function(param, methodVal = 'GET') {
		return new Promise((resolve, reject) => {
			param.v = param.v || '/v1'; //接口版本
			const versionInfo = wx.getAccountInfoSync();
			param.data.env = versionInfo.miniProgram.envVersion; //接口版本
			param.data.shanhaiKey = uni.getStorageSync('shanhaiKey'); 
			param.data.appkey = uni.getStorageSync('appkey'); 
			param.data.group = uni.getStorageSync('group');
			param.data['access-token'] = uni.getStorageSync('access-token');
			uni.request({
				url: _.apiUrl() + param.v + param.url, 
				data: param.data,
				method: methodVal,
				success: (res) => { 
					if(res&&res.statusCode==200){
						if(res.data.code == 199) {
							uni.navigateTo({
							    url: '/pages/my/login'
							});
							return ;
						}
						resolve(res.data)
					}else{
						uni.showToast({
							title:res.message,
							icon:'none'
						})
					}
					
				},
				fail: (error) => {
					reject(error)
				},
				complete: () => {
					uni.hideLoading();
					uni.stopPullDownRefresh()
				}
			});
		})
	}
	_.apiUrl = function() {
		// return 'http://shyy.com/api'
		return 'https://app.shanhaiyouyu.com/api'
	}
	_.imgUrl = function() {
		// return 'http://shyy.com/attachment/'
		return 'https://app.shanhaiyouyu.com/attachment/'
	}
})(serve);


module.exports = {
	request: serve.request,
	apiUrl : serve.apiUrl,
	imgUrl : serve.imgUrl,
}
